<template>
  <div class="login">
    <video src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4" autoplay="autoplay" loop="loop" muted="muted" style="width: 100%; height: 100%; object-fit: fill;" />
    <el-card>
      <div style="display: flex;justify-content: center;margin: 20px 0">
        <img src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png" alt="" style="height: 54px">
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="account">
          <el-input
            v-model="form.account"
            placeholder="账号"
            prefix-icon="el-icon-user-solid"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
            suffix-icon="el-icon-view"
          />
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="form.code"
            placeholder="验证码"
            prefix-icon="el-icon-view"
          >
            <template #suffix>
              <img :src="url" alt="" style="width: 100px;height: 25px; margin-top: 10px;" @click="getyanzheng()">
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" style="width: 100%;height: 54px;" @click="getLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login, yanzheng } from '@/api/user'
import { Message } from 'element-ui'

export default {
  name: 'Login',
  data() {
    return {
      token: '', // 存放token
      url: '', // 验证码图片路径
      form: {
        account: 'shenlingadmin',
        password: '123456',
        code: ''
      },
      rules: {
        account: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      yanzhengma: {
        key: '85DE86BE6711ACA8009049961676695964055',
        0: '85DE86BE6711ACA8009049961676695964055'
      }
    }
  },
  created() {
    this.getyanzheng()
  },
  methods: {
    // 获取验证码
    async getyanzheng() {
      const res = await yanzheng(this.yanzhengma)
      // console.log(res)
      this.url = window.URL.createObjectURL(res)// 用于验证码图片转码操作
    },
    // 登录接口
    async getLogin() {
      // 1、表单验证
      await this.$refs.form.validate()
      const res = await login({
        account: this.form.account,
        password: this.form.password,
        code: this.form.code,
        key: this.yanzhengma.key

      })
      // 判断验证码输入的是否正确 后端返回code为1 验证码错误
      if (res.code == 1) {
        Message.success(res.msg)
      } else {
        // 2、判断验证码 存储token 用户信息 提示成功 跳转页面
        console.log(res)
        this.$store.commit('user/getToken', res.data)
        Message.success('登录成功')
        this.$router.push('/')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login{
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  .el-card{
    position: absolute;
    width: 450px;
    height: 460px;
  }
}

</style>
